package Persistence;

import java.sql.ResultSet;
import java.sql.SQLException;

import Dominion.*;

public class ReviewerDAO extends CrudDAO<Reviewer>{

    @Override
    public Reviewer create(Reviewer revisor) {
        String sqlQuerry = "INSERT INTO Reviewer (email, karma, VIP) VALUES ('"
        		+revisor.getEmail()+"','"+revisor.getKarma()+"','"+revisor.getVIP()+"')";
        try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return null;
    }

    @Override
    public void delete(Reviewer revisor) {
    	 try {
 			AgenteBD.getDbCon().queryUpdate("DELETE FROM Reviewer WHERE email='"+revisor.getEmail()+"'");
 		} catch (SQLException e) {
 			e.printStackTrace();
 		}
    }

    @Override
    public Reviewer read(Reviewer revisor) throws SQLException {
      ResultSet rs = AgenteBD.getDbCon().query(
                "SELECT * FROM Reviewer WHERE email=" + revisor.getEmail()+ "");
        if (rs != null) {
            try {
                while (rs.next()) {
                    revisor.setKarma(rs.getInt("karma"));
                    revisor.setVIP(rs.getInt("VIP"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        rs.close();
        return revisor;
    }

    @Override
    public void update(Reviewer revisor) {
    	 String sqlQuerry="UPDATE Reviewer SET karma='"+revisor.getKarma()
		         +"', VIP='" + revisor.getVIP()
		         + "' WHERE email='" + revisor.getEmail()+ "'";
		try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}       
    }
}
